/*
 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
 */

#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include "link_visual_enum.h"
#include "linkkit_client.h"
#include "linkvisual_client.h"
#ifdef DUMMY_IPC
#include "dummy_ipc.h"
#else
#include "normal_ipc.h"
#endif
#include "sdk_assistant.h"
#include "iot_import.h"
#include "cx_cloud.h"
#include "cx_debug.h"

#define TAG "CX_CLOUD_LINKVISUAL"

cx_cloud_priv_t *cloud_priv;
void *cx_cloud_init(cx_cloud_param_t *param, cx_cloud_cbs *cbs)
{
    cloud_priv = (cx_cloud_priv_t *)malloc(sizeof(cx_cloud_priv_t));
    CHECK_RET_WITH_RET(cloud_priv, NULL);

    memcpy(&cloud_priv->param, param, sizeof(cx_cloud_param_t));
    return cloud_priv;
}

int cx_cloud_deinit(void *hdl)
{
    CHECK_PARAM(hdl, -1);

    cx_cloud_priv_t *priv = (cx_cloud_priv_t *)hdl;
    free(priv);
    return 0;
}

int cx_cloud_connect(void *hdl)
{
    int ret = 0;
    lv_device_auth_s auth_ipc;
    auth_ipc.dev_id = cloud_priv->param.dev_id;//主设备-ipc
    auth_ipc.product_key = cloud_priv->param.product_key;
    auth_ipc.device_name = cloud_priv->param.device_name;
    auth_ipc.device_secret = cloud_priv->param.device_secret;

#ifdef DUMMY_IPC//如果支持宏LINKKIT_INSTANCE
    /* 0.1.初始化虚拟IPC的功能 */

    IpcUnitParam unit;//设置单个IPC的属性
    memset(&unit, 0, sizeof(IpcUnitParam));
    unit.auth = &auth_ipc;

    DummyIpcConfig dummy_ipc_config = {0};//设置整个SDK需要支持的IPC属性
    dummy_ipc_config.ipc_num = 1;
    dummy_ipc_config.ipc_series = &unit;
    unit.picture_interval_s = 30;//设置虚拟IPC生成图片事件的间隔
    dummy_ipc_config.audio_handler = linkvisual_client_audio_handler;//设置虚拟IPC生成音频数据的输入函数
    dummy_ipc_config.video_handler = linkvisual_client_video_handler;//设置虚拟IPC生成视频数据的输入函数
    dummy_ipc_config.picture_handler = linkvisual_client_picture_handler;//设置虚拟IPC生成图片事件数据的输入函数
    dummy_ipc_config.query_record_handler = linkvisual_client_query_record;//设置虚拟IPC查询卡录像的输入函数
    dummy_ipc_config.set_property_handler = linkkit_client_set_property_handler;//设置虚拟IPC生成属性设置的输入函数

    ret = dummy_ipc_start(&dummy_ipc_config);
    if (ret < 0) {
        printf("Start dummy ipc failed\n");
        return -1;
    }

    /* 0.2.初始化在linkvisual中使用虚拟IPC的功能 */
    ret = linkvisual_client_assistant_start(0, &auth_ipc, 0, NULL);
    if (ret < 0) {
        printf("Start linkvisual client dummy ipc assistant failed\n");
        dummy_ipc_stop();
        return -1;
    }
#else
    IpcUnitParam unit;//设置单个IPC的属性
    memset(&unit, 0, sizeof(IpcUnitParam));
    unit.auth = &auth_ipc;
    NormalIpcConfig normal_ipc_config = {0};//设置整个SDK需要支持的IPC属性
    normal_ipc_config.ipc_num = 1;
    normal_ipc_config.ipc_series = &unit;
    unit.picture_interval_s = 30;
    // unit.picture_source = g_image_file;
    normal_ipc_config.audio_handler = linkvisual_client_audio_handler;
    normal_ipc_config.video_handler = linkvisual_client_video_handler;
    normal_ipc_config.picture_handler = linkvisual_client_picture_handler;
    normal_ipc_config.query_record_handler = linkvisual_client_query_record;
    normal_ipc_config.set_property_handler = linkkit_client_set_property_handler;
    ret = normal_ipc_start(&normal_ipc_config);
    if (ret < 0) {
        printf("Start normal ipc failed\n");
        return -1;
    }

    /* 0.2.初始化在linkvisual中使用IPC的功能 */
    ret = linkvisual_client_assistant_start(0, &auth_ipc, 0, NULL);
    if (ret < 0) {
        printf("Start linkvisual client normal ipc assistant failed\n");
        normal_ipc_stop();
        return -1;
    }
#endif // DUMMY_IPC

    /* 1. 初始化LinkVisual的资源 */
    ret = linkvisual_client_init(0, 0, (lv_log_level_e) cloud_priv->param.log_level);
    if (ret < 0) {
        printf("linkvisual_client_init failed\n");
        linkvisual_client_assistant_stop();
#ifdef DUMMY_IPC
        dummy_ipc_stop();
#else
        normal_ipc_stop();
#endif
        return -1;
    }

    /* 2. 初始化linkkit的资源，并长连接到服务器 */
    ret = linkkit_client_start(&auth_ipc, cloud_priv->param.product_secret, 0, NULL);
    if (ret < 0) {
        printf("linkkit_client_start failed\n");
        linkvisual_client_destroy();
        linkvisual_client_assistant_stop();
#ifdef DUMMY_IPC
        dummy_ipc_stop();
#else
        normal_ipc_stop();
#endif
        return -1;
    }
    return 0;
}

int cx_cloud_disconnect(void *hdl)
{
    /* linkkit长连接断开，并释放资源 */
    linkkit_client_destroy();

    /* LinkVisual断开音视频连接，并释放资源 */
    linkvisual_client_destroy();

    linkvisual_client_assistant_stop();
#ifdef DUMMY_IPC
    /* 停止虚拟IPC的功能 */
    dummy_ipc_stop();
#else
    normal_ipc_stop();
#endif // DUMMY_IPC
    return 0;
}

static int cx_cloud_dir_save = CX_CLOUD_DIR_FROM_CLOUD;
int cx_cloud_switch_intercom(void *hdl, int dir_to_cloud)
{
    LOGD(TAG, "%s %d", __func__, dir_to_cloud);
    cx_cloud_dir_save = dir_to_cloud;
    return 0;
}

int cx_cloud_get_intercom_dir(void)
{
    return cx_cloud_dir_save;
}